<template>
  <div>
    <div id="search">
      <el-form :inline="true" :model="CourseSearch" class="demo-form-inline">
        <el-form-item label="部门名称">
          <el-input v-model="CourseSearch.deptName" placeholder="请输入部门名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="header-bth">
      <el-button type="primary" size="small" icon=el-icon-circle-plus-outline @click="showAddScore">添加</el-button>
    </div>
    <div id="role-table">
      <el-table
          :data="tableData"
          stripe
          style="width: 100%">
        <el-table-column label="ID">
          <template slot-scope="scope">
            <span style="margin-left: 20px">{{ scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门名称">
          <template slot-scope="scope">
            <span>{{ scope.row.deptName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" >
          <template slot-scope="scope">
            <span>{{ scope.row.createTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">
            </el-button>
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :current-page="CourseSearch.page"
          :page-size="CourseSearch.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--添加 编辑角色信息弹窗-->
    <el-dialog :title="dialogTitle" :visible.sync="addScoreFormVisible" width="30%" @close="clearInfo">
      <el-form :model="StudentForm" ref="deptInfo" :rules="deptRules" label-width="90px" size="medium">
        <el-input v-model="StudentForm.id" autocomplete="off" v-show="false"></el-input>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="StudentForm.deptName" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit('deptInfo')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import instance from "@/request/request";

export default {
  name: "DeptView",
  data() {
    return {
      tableData: [],
      pageSizes: [3, 5, 10, 15],
      total: 0,
      addScoreFormVisible: false,
      dialogTitle: '',
      StudentForm: {},
      CourseSearch: {  // 搜索数据
        page: 1,
        limit: 5
      },
      deptRules: {
        deptName: [{required: true, message: '请输入部门名称', trigger: 'blur'}],
      },

    }
  },
  created() {
    this.getscoreList()
  },
  methods: {
    //获取角色信息
    getscoreList() {
         instance.get("/dept/getlists",{params:this.CourseSearch}).then(res=>{
            if(res.code==200){
              this.tableData=res.data.dept
              this.total=res.data.total
              return
            }
            this.$message.error(res.msg)
         }).catch(erro=>{
           this.$message.error("请求失败！")
         })
    },
    // 编辑角色
    handleEdit(index, row) {
      this.dialogTitle = "编辑部门"
      this.addScoreFormVisible= true
      var dept=Object.assign({},row)
      this.StudentForm=dept
    },
    //更新和添加用户信息
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
            instance.post("/dept/addOrupdate",this.StudentForm).then(res=>{
              if (res.code==200){
                this.$message.success(res.msg)
                this.addScoreFormVisible=false
                this.getscoreList()
                return
              }
              this.$message.warning(res.msg)
            }).catch(erro=>{
              this.$message.error("请求失败")
            })

        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除角色
    handleDelete(id) {
      this.$confirm('确认要删除？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
         instance.delete("/dept/delete/"+id).then(res=>{
           if(res.code==200){
             this.$message.success(res.msg)
             this.getscoreList()
             return
           }
           this.$message.warning(res.msg)
         }).catch(erro=>{
           this.$message.error("请求失败！")
         })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        })
      })
    },
    // 搜索角色
    onSearchSubmit() {
      this.CourseSearch.page = 1
      this.CourseSearch.limit = 5
      this.getscoreList()
    },
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.CourseSearch.limit = val;
      this.getscoreList()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.CourseSearch.page = val;
      this.getscoreList()
    },
    // 显示弹出等
    showAddScore() {
      this.dialogTitle = "添加部门"
      this.addScoreFormVisible = true
      this.StudentForm = {}
    },
    //清空校验规则
    clearInfo() {
      this.$refs.deptInfo.clearValidate()
    },
  },
}
</script>

<style scoped>
#search {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #eee;
}

#search, #header-bth, #nav-page {
  margin: 15px;
}

#role-table {
  width: 100%;
}
</style>
